
#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"
#include "fr_lv_customer_page.h"  
#include "app_lvgl.h"
//#include "user_bt_call.h"
lv_obj_t *prj_parent_cont = NULL;
lv_obj_t *prj_prev_cont = NULL;
lv_obj_t *prj_float_cont = NULL;
lv_obj_t *prj_call_cont = NULL;
lv_obj_t *prj_flow_cont = NULL;
lv_obj_t *prj_flexible_cont = NULL;
extern lv_obj_t * obj_song_context;
lv_group_t *prj_group = NULL;
lv_obj_t *group_tail_obj = NULL;
lv_api_t ui_api;
main_program_env_t env;

lv_font_t bin_font;
lv_point_t sys_scroll_sum;

// call flag
bool coming_call_ok = false;
bool call_flag = false;

bool vers_fast_sroll_flag = false;

bool src_auto_load_enable = true;

extern bool frame_log_enable;

uint8_t dsp_ipc_cmd;
uint8_t page_hor_id = 0;
#define HOR_PAGE_TOTAL (fr_lv_get_custom_page_length())

extern void fr_lv_app_calculator(lv_obj_t *parent, lv_point_t *top);

extern void fr_hwd_customize_watchface(lv_obj_t *parent, lv_point_t *top);


extern void fr_lv_app_mainUI0_func(lv_obj_t *parent,lv_point_t *top);
extern void fr_lv_app_mainUI1_func(lv_obj_t *parent,lv_point_t *top);
extern void fr_lv_app_mainUI2_func(lv_obj_t *parent,lv_point_t *top);
extern void fr_lv_app_mainUI3_func(lv_obj_t *parent,lv_point_t *top);
void fr_lv_app_368448_test1_func(lv_obj_t *parent,lv_point_t *top);

uint8_t lv_ui_node_depth(void);


uint16_t img_m_size;
uint16_t scr_circle_radius;
uint16_t scr_circle_m_radius;
lv_point_t ori_p[34];

ui_func pri_ui_group[] =
{
	//fr_lv_app_exercise_default_list_func,
		fr_main_roll_cont,
//		fr_lv_app_starsky_func,
#if 0
		fr_lv_app_upgrade_func,
		ui_dialing_function,
		lv_app_msg_win,
		lv_sport_detail_disp,
		fr_lv_app_charge_func,
		UI_dial_phone_book,
#endif
};



LV_ATTRIBUTE_LARGE_CONST const ui_func style_func_tab[] =
{
//	fr_lv_app_starsky_func,
//	fr_lv_arc_list_func,
//	fr_lv_app_circle_func,
//	fr_lv_track_roller_func,
//	fr_lv_list_menu_func,
#if 0
	fr_lv_app_listview_func,
	fr_lv_app_palace_of_icon_func,
	fr_lv_app_double_circle,
#endif
};


//--------------------------------------------




//--------------------------------------------




/*-----------------
 *  PUBLIC FONT
 *----------------*/

//------font bin address table define here ---
void **font_addr = (void *)0x20200000;

/*Initialize a public general font descriptor*/
#if LV_VERSION_CHECK(8, 0, 0)
LV_ATTRIBUTE_LARGE_CONST const lv_font_t arialuni_bpp1_32 = {
#else
lv_font_t arialuni_bpp1_32 = {
#endif
	.get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt, /*Function pointer to get glyph's data*/
	.get_glyph_bitmap = lv_font_get_bitmap_fmt_txt, /*Function pointer to get glyph's bitmap*/
	.line_height = 45,								/*The maximum line height required by the font*/
	.base_line = 10,								/*Baseline measured from the bottom of the line*/
#if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0)
	.subpx = LV_FONT_SUBPX_NONE,
#endif
#if LV_VERSION_CHECK(7, 4, 0)
	.underline_position = -3,
	.underline_thickness = 2,
#endif
	.dsc = NULL /*The custom font data. Will be accessed by `get_glyph_bitmap/dsc` */
};


void lv_init_bin_font(lv_font_t *font, const void *dsc, const lv_font_t *scr)
{
	font->get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt; /*Function pointer to get glyph's data*/
	font->get_glyph_bitmap = lv_font_get_bitmap_fmt_txt; /*Function pointer to get glyph's bitmap*/
	font->line_height = scr->line_height;				 /*The maximum line height required by the font*/
	font->base_line = scr->base_line;					 /*Baseline measured from the bottom of the line*/
#if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0)
	font->subpx = scr->subpx;
#endif
#if LV_VERSION_CHECK(7, 4, 0)
	font->underline_position = scr->underline_position;
	font->underline_thickness = scr->underline_thickness;
#endif
	font->dsc = dsc; /*The custom font data. Will be accessed by `get_glyph_bitmap/dsc` */
};




void extern_ui_api_config(void)
{

	ui_api.f_lv_obj_create = lv_obj_create;
	ui_api.f_lv_obj_clean = lv_obj_clean;

	// style
	ui_api.f_lv_obj_set_style_bg_color = lv_obj_set_style_bg_color;
	ui_api.f_lv_obj_set_style_pad_all = lv_obj_set_style_pad_all;
	ui_api.f_lv_obj_set_style_radius = lv_obj_set_style_radius;
	ui_api.f_lv_obj_set_style_border_width = lv_obj_set_style_border_width;

	// obj prop
	ui_api.f_lv_obj_align = lv_obj_align;
	ui_api.f_lv_obj_align_to = lv_obj_align_to;
	ui_api.f_lv_obj_set_pos = lv_obj_set_pos;
	ui_api.f_lv_obj_set_size = lv_obj_set_size;
	ui_api.f_lv_obj_set_scrollbar_mode = lv_obj_set_scrollbar_mode;

	// img
	ui_api.f_lv_img_create = lv_img_create;
	ui_api.f_lv_img_set_src = lv_img_set_src;
	ui_api.f_lv_img_set_pivot = lv_img_set_pivot;
	ui_api.f_lv_img_set_angle = lv_img_set_angle;

	// timer
	ui_api.f_lv_timer_create = lv_timer_create;
	ui_api.f_lv_timer_del = lv_timer_del;

	// event
	ui_api.f_lv_event_send = lv_event_send;
	ui_api.f_lv_obj_add_event_cb = lv_obj_add_event_cb;
	ui_api.f_lv_event_get_user_data = lv_event_get_user_data;
	ui_api.f_lv_event_get_code = lv_event_get_code;
	ui_api.f_lv_event_get_target = lv_event_get_target;
	ui_api.f_lv_event_get_param = lv_event_get_param;

	// parent & child
	ui_api.f_lv_obj_get_child = lv_obj_get_child;
	ui_api.f_lv_obj_get_child_id = lv_obj_get_child_id;
	ui_api.f_lv_obj_get_parent = lv_obj_get_parent;

	// font
	ui_api.f_lv_label_create = lv_label_create;
	ui_api.f_lv_label_set_text = lv_label_set_text;
	ui_api.f_lv_obj_set_style_text_font = lv_obj_set_style_text_font;

	ui_api.f_lv_mainUI_select_entry = NULL; // lv_app_clock_ui_select;

	ui_api.prj_parent = &prj_parent_cont;

	ui_api.p_sync_cm3 = &sync_cm3_data;

	// line & src
	ui_api.f_lv_style_init = lv_style_init;
	ui_api.f_lv_style_set_line_width = lv_style_set_line_width;
	ui_api.f_lv_style_set_line_color = lv_style_set_line_color;
	ui_api.f_lv_style_set_line_rounded = lv_style_set_line_rounded;
	ui_api.f_lv_line_create = lv_line_create;
	ui_api.f_lv_line_set_points = lv_line_set_points;
	ui_api.f_lv_obj_add_style = lv_obj_add_style;
	ui_api.f_lv_arc_create = lv_arc_create;
	ui_api.f_lv_obj_set_style_arc_width = lv_obj_set_style_arc_width;
	ui_api.f_lv_obj_set_style_arc_color = lv_obj_set_style_arc_color;
	ui_api.f_lv_arc_set_end_angle = lv_arc_set_end_angle;
	ui_api.f_lv_arc_set_bg_angles = lv_arc_set_bg_angles;

	ui_api.f_lv_trigo_sin = lv_trigo_sin;
	ui_api.f_lv_trigo_cos = lv_trigo_cos;

	ui_api.f_lv_label_set_text_fmt = lv_label_set_text_fmt;

	ui_api.font_min = (lv_font_t *)&lv_font_montserrat_14;
	ui_api.font_mid = NULL; //&lv_font_montserrat_22;
	ui_api.font_max = NULL; ///&lv_font_montserrat_32;

	ui_api.f_lv_font_get_glyph_dsc_fmt_txt = lv_font_get_glyph_dsc_fmt_txt;
	ui_api.f_lv_font_get_bitmap_fmt_txt = lv_font_get_bitmap_fmt_txt;

	ui_api.f_lv_tick_get = lv_tick_get;

	ui_api.f_lv_obj_is_valid = lv_obj_is_valid;

	ui_api.f_fr_system_get_time_week = fr_system_get_time_week;

	ui_api.f_lv_obj_set_style_text_color = lv_obj_set_style_text_color;

	ui_api.f_lv_obj_remove_style = lv_obj_remove_style;
	ui_api.f_lv_arc_set_angles = lv_arc_set_angles;
	ui_api.f_lv_obj_clear_flag = lv_obj_clear_flag;
	ui_api.f_lv_label_set_recolor = lv_label_set_recolor;
	ui_api.f_lv_obj_set_style_text_letter_space = lv_obj_set_style_text_letter_space;
	ui_api.f_lv_obj_set_style_text_line_space = lv_obj_set_style_text_line_space;

	ui_api.f_lv_obj_set_width = lv_obj_set_width;
	ui_api.f_lv_obj_set_style_text_align = lv_obj_set_style_text_align;
}

extern void extern_flash_read(uint8_t *Buffer, uint32_t flash_Address, uint32_t Length);
void fr_load_app_from_extern_flash(uint32_t flash_addr)
{
	//load code from extern flash
	//printf("wf flash addr 0x%x: \n",flash_addr);
	uint32_t wf_buffer[256];
	void *dst = (void *)(0x1FFFE000);
	uint32_t src = flash_addr;
	while((uint32_t)dst < (0x1FFFE000 + 0x2000))
	{
		//printf("dst 0x%x: src:%x \n",dst,src);
		extern_flash_read((void *)wf_buffer, src, 0x400);
		memcpy(dst,wf_buffer,0x400);
		src += 0x400;
		dst += 0x400;
	}

	//main_program_env_t env;
	env.lv_api = &ui_api;
	env.img_addr = (void *)flash_addr;
	env.debug_api.__printf =  printf;
	env.mem_api.__free = lv_mem_free;
	env.mem_api.__malloc = lv_mem_alloc;
	
	app_entry *entry = (app_entry *)0x1FFFE000;
	(*entry)(&env);
}

void fr_load_watch_face_from_ext_flash(lv_obj_t *parent,void *arg)
{

	//main_program_env_t env;
	env.parent = parent;
	env.arg = arg;
	env.debug_api.__printf =  printf;
	env.mem_api.__free = lv_mem_free;
	env.mem_api.__malloc = lv_mem_alloc;
	
	app_entry *entry = (app_entry *)0x1FFFE000;
	(*(entry+1))(&env);
}


void return_function(lv_obj_t *parent)
{

	if (dsp_get_ui_scene_default_style())
	{
		
	}
	else
	{
		if (dsp_get_ui_scene_page_id() >= 4)
			dsp_store_ui_scene(UI_MAIN_FLOAT, 0, dsp_get_ui_scene_page_id(), 0, 0);
		else
			dsp_store_ui_scene(UI_MAIN_FLOAT, 0, 0, 0, 0);
		pri_ui_group[dsp_get_ui_scene_mode()](parent, NULL);
	}
}


static void main_event_handler(lv_event_t *e)
{
	lv_event_code_t event = lv_event_get_code(e);
	lv_obj_t *parent = lv_event_get_target(e);

	if(!lv_obj_is_valid(parent))
		return;

	if(event == LV_EVENT_CALL_IN)
	{
		//来电
		if(!lv_obj_is_valid(prj_call_cont) && (prj_call_cont==NULL))
		{
			prj_call_cont = lv_obj_create(parent);
			//fr_lv_app_dialing_function(prj_call_cont,NULL);
		}
		
	}
	else if(event == LV_EVENT_FLEXIBLE_IN)
	{
		printf("LV_EVENT_FLEXIBLE_IN\r\n");
	 	if(prj_flexible_cont != NULL)
		{
			lv_obj_del(prj_flexible_cont);
			prj_flexible_cont = NULL;
		}
		prj_flexible_cont = lv_obj_create(parent);
		//fr_flexible_msg_set_type(FLEXIBLE_PHONE_CALL);
		//fr_lv_flexible_msg_func(prj_flexible_cont,NULL);
	}
	else if(event == LV_EVENT_CALL_OK)
	{
		printf("LV_EVENT_CALL_OK\r\n");
		if(lv_obj_is_valid(prj_flexible_cont)&&(prj_flexible_cont!=NULL))
			return;
		//接通
		if(prj_call_cont != NULL)
		{
			lv_obj_del(prj_call_cont);
			prj_call_cont = NULL;
		}
		prj_call_cont = lv_obj_create(parent);
		//fr_lv_app_dialing_function(prj_call_cont,NULL);

	}
	else if(event == LV_EVENT_CALL_HANDUP || event == LV_EVENT_CALL_REJECT)
	{
		//lv_ui_node_return_to_prev_node();
		printf("%s %d \r\n", __func__, __LINE__);
		if(prj_call_cont != NULL)
		{
			lv_obj_del(prj_call_cont);
        	prj_call_cont = NULL;
		}
		if(prj_flexible_cont != NULL)
		{
			lv_obj_del(prj_flexible_cont);
			prj_flexible_cont = NULL;
		}
		printf("event_call_handup\r\n");
	}	
	else if (event == LV_EVENT_ANCS_MSG_IN)
	{
		#if 0
		//信息提示
		if(!lv_obj_is_valid(prj_float_cont))
		{
			prj_float_cont = lv_obj_create(parent);
			lv_app_msg_win(prj_float_cont, NULL);
		}
		else
		{
			lv_event_send(prj_float_cont, LV_EVENT_ANCS_MSG_IN, NULL);
		}
		#else
		printf("LV_EVENT_ANCS_MSG_IN\r\n");
		if(!lv_obj_is_valid(prj_flexible_cont))
		{
			
			prj_flexible_cont = lv_obj_create(parent);
//			fr_flexible_msg_set_type(FLEXIBLE_MESSAGE);
//			fr_lv_flexible_msg_func(prj_flexible_cont,NULL);
		}
		else
		{
			lv_event_send(prj_flexible_cont, LV_EVENT_ANCS_MSG_IN, NULL);
		}
		#endif

	}
	else if (event == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);

		//fputc('A',NULL);
		if((*key == 129) || (*key == 130) || (*key == 6))
		{
			uint32_t group_obj_number = lv_group_get_obj_count(prj_group);
			while(group_obj_number > 1)
			{
				lv_group_focus_next(prj_group);
				lv_obj_t * focused_obj = lv_group_get_focused(prj_group);
				if (focused_obj != parent)
				{
					//fputc('B',NULL);
					if(lv_obj_is_valid(focused_obj))
					{
						//fputc('C',NULL);
						if(group_tail_obj == focused_obj)
							lv_group_send_data(prj_group, *key);
					}
				}
				else
				{
					break;
				}
				group_obj_number --;
			}
			lv_group_focus_obj(parent);
			//fputc('D',NULL);
		}else{
			lv_event_send(prj_prev_cont,LV_EVENT_KEY,key);
		}
 
	}
}

void fr_lv_style_switch_disp(void)
{
	//style switch 
	if (dsp_get_ui_scene_mode() == 0)
	{
		if (dsp_get_ui_scene_page_id() == 0)
		{
			dsp_set_ui_scene_default_style(1);
			dsp_store_ui_scene(1, 0, 0, 0, 0);
		}
		else
		{
			if(lv_ui_node_depth() > 0)
			{
				lv_ui_node_stack_init();
			}
			page_hor_id = 0;
			dsp_store_ui_scene(0, 0, 0, 0, 0);
		}
	}
	else
	{
		if(lv_ui_node_depth() > 0)
		{
			lv_ui_node_stack_init();
		}
		dsp_set_ui_scene_default_style(0);
		dsp_store_ui_scene(0, 0, 0, 0, 0);
	}
	//================================================
	//lv_obj_clean(prj_parent_cont);
	// prj_prev_cont = lv_obj_create(prj_parent_cont);
	fr_del_float_win();
	lv_obj_del(prj_prev_cont);
	prj_prev_cont = lv_obj_create(prj_parent_cont);
	lv_obj_move_background(prj_prev_cont);

	if (dsp_get_ui_scene_default_style())
	{
		style_func_tab[fr_system_get_menu_style()](prj_prev_cont, NULL);
		dsp_set_ui_scene_default_style(1);
	}
	else
	{
		pri_ui_group[dsp_get_ui_scene_mode()](prj_prev_cont, NULL);
		dsp_set_ui_scene_default_style(0);
	}
}


void user_jump_page(ui_func func)
{
	 lv_obj_remove_event_cb(prj_prev_cont,NULL);
	 lv_obj_clean(prj_parent_cont);
     prj_prev_cont = lv_obj_create(prj_parent_cont);
	//lv_obj_clean(prj_prev_cont);
	 func(prj_prev_cont,NULL);
}

void lv_prj_main(void)
{
	printf("lv app main\r\n");

	extern_ui_api_config();

	#if 1
	///初始化自定义卡片
	fr_lv_custom_page_init();
//	return;
	prj_parent_cont = lv_obj_create(lv_scr_act());
	lv_obj_set_style_pad_all(prj_parent_cont, 0, 0);
	lv_obj_set_style_border_width(prj_parent_cont, 0, 0);
	lv_obj_set_size(prj_parent_cont, LV_PCT(100), LV_PCT(100));
	lv_obj_add_flag(prj_parent_cont, LV_OBJ_FLAG_SCROLL_ONE);
	lv_obj_set_scroll_snap_x(prj_parent_cont, LV_SCROLL_SNAP_CENTER);
	lv_obj_set_scroll_snap_y(prj_parent_cont, LV_SCROLL_SNAP_CENTER);
	lv_obj_set_scrollbar_mode(prj_parent_cont, LV_SCROLLBAR_MODE_OFF);
	lv_obj_clear_flag(prj_parent_cont, LV_OBJ_FLAG_SCROLL_ELASTIC);
	lv_obj_add_event_cb(prj_parent_cont, main_event_handler, LV_EVENT_ALL, NULL);

//    lv_obj_t *cont_parent = lv_obj_create(prj_parent_cont);
//    lv_obj_set_size(cont_parent, LV_PCT(100), LV_PCT(100));
//    lv_obj_set_style_bg_color(cont_parent,lv_color_hex(0xff0000),0);
	// fr_lv_list_test_func(cont_parent);
	// return;

	 prj_group = lv_group_create();
	 lv_indev_set_group(indev_keypad, prj_group);
	 lv_group_add_obj(prj_group, prj_parent_cont);
	 prj_prev_cont = lv_obj_create(prj_parent_cont);
	 //fr_list_page(prj_prev_cont,NULL);
	 fr_logo_page_func(prj_prev_cont,NULL);
     printf("lv app main end\r\n");
	//printf("anim_time :%d %d\r\n",lv_obj_scroll_anim_time_min(),lv_obj_scroll_anim_time_max());
//	lv_obj_set_scroll_anim_time(1000,2000);
	#else
	extern void fr_lv_instrument_panel_func(lv_obj_t *parent, lv_point_t *top);
	prj_prev_cont = lv_obj_create(prj_parent_cont);
	fr_lv_instrument_panel_func(prj_prev_cont,NULL);
	#endif
	
}


// ------------------------------------------------------------------------------
//  frame
//-------------------------------------------------------------------------------
// config
// 在此配置框架支持的左，上，下叠加页面的滑动
#define FR_MAIN_ROLL_UP 0x01
#define FR_MAIN_ROLL_DOWN 0x02
#define FR_MAIN_ROLL_LEFT 0x04

uint8_t fr_main_roll_get_dir(void)
{
	//return FR_MAIN_ROLL_UP | FR_MAIN_ROLL_DOWN | FR_MAIN_ROLL_LEFT;
	//return FR_MAIN_ROLL_UP | FR_MAIN_ROLL_DOWN;
	return 0;
}

void fr_ui_float_up_func(lv_obj_t *parent, lv_point_t *top)
{

	lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100));
	lv_obj_set_style_pad_all(parent, 0, 0);
	lv_obj_set_style_bg_color(parent, lv_color_make(0x00, 0x00, 0xff), 0);
	lv_obj_set_style_border_width(parent, 0, 0);

}


void fr_ui_float_down_func(lv_obj_t *parent, lv_point_t *top)
{

	lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100));
	lv_obj_set_style_pad_all(parent, 0, 0);
	lv_obj_set_style_bg_color(parent, lv_color_make(0xff, 0x00, 0x00), 0);
	lv_obj_set_style_border_width(parent, 0, 0);

}


void fr_ui_float_left_func(lv_obj_t *parent, lv_point_t *top)
{

	lv_obj_set_size(parent, LV_PCT(50), LV_PCT(100));
	lv_obj_set_style_pad_top(parent, 0, 0);
	lv_obj_set_style_pad_left(parent, 0, 0);
	lv_obj_set_style_bg_color(parent, lv_color_make(0x00, 0xff, 0x00), 0);
	lv_obj_set_style_border_width(parent, 0, 0);

}

void fr_ui_custom_cards_func(lv_obj_t *parent, lv_point_t *top)
{

	lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100));
	lv_obj_set_style_pad_all(parent, 0, 0);
	lv_obj_set_style_bg_color(parent, lv_color_make(0xff, 0xff, 0xff), 0);
	lv_obj_set_style_border_width(parent, 0, 0);

}

ui_func fr_ui_func_get_float_up(void)
{
	//return fr_lv_message_hint_page_func;	//fr_ui_float_up_func;
	return NULL;	
}

ui_func fr_ui_func_get_float_down(void)
{
	//return fr_lv_dorp_down_func;	//fr_ui_float_down_func;
	return NULL;	
}

ui_func fr_ui_func_get_float_left(void)
{
//	return fr_lv_home_short_menu_func;	//fr_ui_float_left_func;
	return NULL;	
}



static void fr_lv_watchface_longpress_cb(lv_event_t *e)
{

//	lv_ui_node_alpha_fade_add(prj_prev_cont, prj_prev_cont, fr_ui_get_watchface(), fr_lv_dial_preview_func, 0);

}

void fr_main_roll_event_call_back(lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);
	lv_obj_t *obj = lv_event_get_target(e);

	if (!lv_obj_is_valid(obj))
		return;

	if (code == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);
		//printf("obj->wf,key %d,%d,%d \n",*key,dsp_get_ui_scene_page_id(),page_hor_id);
		if (((dsp_get_ui_scene_page_id()) == 0) && (page_hor_id == 0))
		{
			// 编码器切换表盘
			uint8_t ui_face_id = fr_system_get_watch_face();
			if (*key == 130)
			{
				ui_face_id++;
//				ui_face_id %= fr_ui_watchface_total() - 1;
			}
			else if (*key == 129)
			{
				if (ui_face_id > 0)
					ui_face_id --;
//				else
//					ui_face_id = fr_ui_watchface_total() - 2;
			}

			lv_obj_t *obj_child_0 = lv_obj_get_child(obj, 1);
			if (lv_obj_is_valid(obj_child_0))
			{
				lv_obj_clean(obj_child_0);

				fr_system_set_watch_face(ui_face_id);
				ui_func func;
//				func = fr_ui_get_watchface();
				func(obj_child_0, NULL);
//				fr_lv_custom_page_set_watch_face(ui_face_id);
			}
		}

	}
	else if (code == LV_EVENT_DELETE)
	{
		lv_group_remove_obj(obj);
		group_tail_obj = lv_group_get_focused(prj_group);
		//m3_log("obj->wf,group remove \n");
	}
}

void fr_main_roll_incr_page_hor_id(void)
{
	page_hor_id++;
	page_hor_id %= HOR_PAGE_TOTAL;
}
void fr_main_roll_decr_page_hor_id(void)
{
	if (page_hor_id > 0)
		page_hor_id--;
	else
		page_hor_id = (HOR_PAGE_TOTAL - 1);
}


void fr_main_roll_left_end(void)
{
	fr_main_roll_decr_page_hor_id();
	if (page_hor_id == 0)
	{
		dsp_store_ui_scene(0, 0, 0, 0, 0);
	}
	else
	{
		dsp_store_ui_scene(0, 0, (3 + page_hor_id), 0, 0);
	}
}

void fr_main_roll_right_end(void)
{
	fr_main_roll_incr_page_hor_id();
	if (page_hor_id == 0)
	{
		dsp_store_ui_scene(0, 0, 0, 0, 0);
	}
	else
	{
		dsp_store_ui_scene(0, 0, (3 + page_hor_id), 0, 0);
	}
}


// 当前主页面的向右滚动处理函数
void fr_main_roll_func(lv_obj_t *parent, lv_point_t *top)
{
	lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100));
	lv_obj_set_style_pad_all(parent, 0, 0);
	lv_obj_set_style_bg_color(parent, lv_color_make(0x00, 0xff, 0x00), 0);
	lv_obj_set_style_border_width(parent, 0, 0);

	// 在此切换主页面表盘页面加载那个表盘
	//====================================================
//	uint8_t watch_face_id = fr_system_get_watch_face();
//	if (watch_face_id < fr_ui_watchface_total())
//	{
// 		fr_lv_custom_page_set_watch_face(watch_face_id);
//	}
	//====================================================

	lv_obj_t *cont_left = lv_obj_create(parent);

	lv_obj_set_size(cont_left, LV_PCT(100), LV_PCT(100));
	ui_func func;
	//func = get_prior_element(page_hor_id);
	//func(cont_left,NULL);

	lv_obj_t *cont_curr = lv_obj_create(parent);
	lv_obj_set_size(cont_curr, LV_PCT(100), LV_PCT(100));
	func = get_cur_element(page_hor_id);
	//printf("id%d,f:%x",page_hor_id,func);
	func(cont_curr,top);
	// if (page_hor_id == 0)
	// {
	// 	lv_obj_add_event_cb(cont_curr, fr_lv_watchface_longpress_cb, LV_EVENT_LONG_PRESSED, NULL);
	// }

	lv_obj_t *cont_right = lv_obj_create(parent);
	lv_obj_set_size(cont_right, LV_PCT(100), LV_PCT(100));
	//func = get_next_element(page_hor_id);
	//func(cont_right,NULL);

	lv_obj_scroll_to_view(cont_curr, LV_ANIM_OFF);


}


// 获取当前首页面对应的ID
uint8_t fr_main_get_page_hor_index(void)
{
	return page_hor_id;
}


void fr_frame_top_scroll_begin(lv_obj_t * obj)
{

}

void fr_frame_top_scrolling(lv_obj_t * obj)
{
	lv_coord_t temp = lv_obj_get_scroll_top(obj);
	//printf("top temp:%d \n",temp);
	lv_opa_t zoom = lv_map(temp,0,lv_disp_get_ver_res(lv_disp_get_default()),LV_OPA_90,LV_OPA_0);
	lv_alpha_fade_set(lv_obj_get_child(obj,0),zoom);

}

void fr_frame_top_scroll_end(lv_obj_t * obj)
{
	//printf("scroll end \n");
	lv_coord_t temp = lv_obj_get_scroll_top(obj);
	lv_opa_t zoom = lv_map(temp,0,lv_disp_get_ver_res(lv_disp_get_default()),LV_OPA_100,LV_OPA_0);
	lv_alpha_fade_set(lv_obj_get_child(obj,0),zoom);
}


void fr_frame_bottom_scroll_begin(lv_obj_t * obj)
{
	lv_obj_t * child_1;
	if(lv_obj_get_child_cnt(obj) > 0)
	{
		child_1 = lv_obj_get_child(obj,1);
		if(lv_obj_is_valid(child_1))
			lv_obj_set_style_bg_opa(child_1,LV_OPA_30,0);
	}

	//lv_alpha_fade_set(lv_obj_get_child(obj,1),LV_OPA_50);
}

void fr_frame_bottom_scrolling(lv_obj_t * obj)
{

	//lv_coord_t temp = lv_obj_get_scroll_top(obj);
	//printf("bottom temp:%d \n",temp);
	//lv_alpha_fade_set(lv_obj_get_child(obj,1),lv_map(temp,0,lv_disp_get_ver_res(lv_disp_get_default()),LV_OPA_0,LV_OPA_100));

}

void fr_frame_bottom_scroll_end(lv_obj_t * obj)
{
	// lv_obj_t * child_1;
	// if(lv_obj_get_child_cnt(obj) > 0)
	// {
	// 	child_1 = lv_obj_get_child(obj,1);
	// 	if(lv_obj_is_valid(child_1))
	// 	lv_obj_set_style_bg_opa(child_1,LV_OPA_100,0);
	// }
}

// ------------------------------------------------------------------------------
// ui node
//-------------------------------------------------------------------------------

// 节点页面的函数表，所有子页面函数都可以加入函数表以便调用
static LV_ATTRIBUTE_LARGE_CONST const ui_func ui_func_tab[] =
{
	fr_lv_86box_page1_func,
    fr_lv_86box_page2_func,
//    fr_lv_86box_page3_func,
//    fr_lv_86box_page4_func,
//    fr_lv_86box_page5_func,
//    fr_lv_86box_page6_func,

};

// 节点lib依赖的函数
// open function

ui_func *lv_ui_node_get_ui_func_head(void)
{
	return (ui_func *)ui_func_tab;
}

uint16_t lv_ui_node_get_ui_group_size(void)
{
	return sizeof(ui_func_tab) / 4;
}

void lv_ui_node_save_node_update(void)
{
	//fr508x dsp
	// dsp_ipc_cmd = M3_DRIVER_DAILY_DATA;
	// ipc_msg_with_payload_send(IPC_MSG_M3_INPUT, &dsp_ipc_cmd, 1, (uint8_t *)&sync_cm3_data, sizeof(ipc_daily_data_t), NULL);
}

lv_ui_node_t *lv_ui_node_get_node_stack(void)
{
	return sync_cm3_data.ui_info.ui_node_stack;
}

uint8_t lv_ui_node_stack_size(void)
{
	return UI_NODE_MAX_DEPTH;
}

uint8_t lv_ui_node_depth(void)
{
	return sync_cm3_data.ui_info.ui_node_depth;
}

void lv_ui_node_depth_set(uint8_t depth)
{
	sync_cm3_data.ui_info.ui_node_depth = depth;	
}

void lv_ui_node_hor_return_head(lv_obj_t *parent, lv_ui_node_t *e)
{
	//printf("1 prj_p %x prev_p %x, parent %x, parent->p %x \n", prj_parent_cont, prj_prev_cont, parent, parent->parent);
	lv_obj_clean(parent);
	//printf("return node head:%d page id %d \n",dsp_get_ui_scene_mode(),fr_main_get_page_hor_index());
	//返回首页，需要根据首页定位返回
	if(dsp_get_ui_scene_mode() == 0)
	{
		pri_ui_group[dsp_get_ui_scene_mode()](parent, &e->child_0_top);
	}
	else
	{
		ui_func *ui_func_group = lv_ui_node_get_ui_func_head();
		ui_func_group[e->child_0_id](parent, &e->child_0_top);
	}

}

void lv_ui_node_ver_return_head(lv_obj_t *parent, lv_ui_node_t *e)
{
}

void lv_ui_node_return_home(void)
{

	lv_ui_node_t *p_node_stack = lv_ui_node_get_node_stack();
	lv_ui_node_t *e;
	*e = p_node_stack[0];
	lv_ui_node_stack_init();
	lv_obj_remove_event_cb(prj_prev_cont,NULL);
	lv_obj_clean(prj_prev_cont);
	lv_obj_t *parent = lv_obj_create(prj_prev_cont);
	ui_func *ui_func_group = lv_ui_node_get_ui_func_head();
	ui_func_group[e->child_0_id](parent, &e->child_0_top);
}

//lvgl task event
extern bool screensaver_lock_flags;
void gui_task_queue_callback(gui_task_msg_t * event)
{
    switch(event->msg_type)
    {
        case BUTTON_KEY_EVT:
            {
				if(!gui_task_suspend_state())
				{
					gui_task_suspend();
				}
            }
            break;
        
        case ENCODE_KEY_EVT:
            {
//				if(screensaver_lock_flags)
//				{
//					if(lv_obj_is_valid(prj_flow_cont))
//					{
//						lv_obj_del(prj_flow_cont);
//						prj_flow_cont = NULL;
//						screensaver_lock_flags = false;
//					}
//					break;
//				}
                key_code = (event->param[0]==129)?KEY1_CODE:KEY3_CODE;
				//printf("key_code:%d\r\n",key_code);
            }
            break;
		case BUTTON_KEY5_EVT:
            {
//				if(!gui_task_suspend_state())
//				{
//					gui_task_suspend();
//				}
				key_code = KEY5_CODE;
            }
            break;
        case BUTTON_KEY3_EVT:
            {
				key_code = KEY3_CODE;
//				if(fr_lv_get_charge_ui_state())
//					return;

                //fr_lv_style_switch_disp();

            }
            break;

        case BUTTON_KEY1_EVT:
            {
				key_code = KEY1_CODE;
            }
            break;
			
		case BUTTON_KEY4_EVT:
            {
				key_code = KEY4_CODE;
            }
            break;
        
        case MESSAGE_IN_EVT:
            {
                if(lv_obj_is_valid(prj_parent_cont))
				{
                	lv_event_send(prj_parent_cont, LV_EVENT_ANCS_MSG_IN, NULL);
				}
            }
            break;

        case PHONE_CALL_IN_EVT:
            {
				// uint8_t call_mode = user_get_call_mode();
                // if(lv_obj_is_valid(prj_parent_cont))
				// {
				// 	if(call_mode!=5)
				// 	{
				// 		lv_event_send(prj_parent_cont, LV_EVENT_CALL_IN, NULL);
				// 	}else{
				// 		lv_event_send(prj_parent_cont, LV_EVENT_FLEXIBLE_IN, NULL);	
				// 	}
				// }
            }
            break;
		
        case PHONE_CALL_OK_EVT:
            {
				//printf("-->PHONE_CALL_OK_EVT 1\r\n");
                if(lv_obj_is_valid(prj_parent_cont))
				{
					//printf("-->PHONE_CALL_OK_EVT 2\r\n");
					lv_event_send(prj_parent_cont, LV_EVENT_CALL_OK, NULL);
				}                
            }
            break;

        case PHONE_CALL_REJECT_EVT:
            {
				//printf("-->PHONE_CALL_REJECT_EVT 1\r\n");
                if(lv_obj_is_valid(prj_parent_cont))
				{
					//printf("-->PHONE_CALL_REJECT_EVT 2\r\n");
					lv_event_send(prj_parent_cont, LV_EVENT_CALL_REJECT, NULL);
				}                  
            }
            break;

		case MUSIC_CONTROL_EVT:
			{
//                if(lv_obj_is_valid(obj_song_context))
//				{
//					lv_event_send(obj_song_context, LV_EVENT_MSUIC_CONTEXT_IN, NULL);//
//				}
			}
			break;

        case POWER_CHARGE_IN_EVT:
            {
                //offscreen_dial_charge_create();
            }
            break;

        case POWER_CHARGE_OUT_EVT:
            {
               // offscreen_dial_charge_clean();
            }
            break;

		case SCREENSAVER_IN_EVT:
			{
//				if(lv_obj_is_valid(prj_parent_cont) 
//					&& (dsp_get_ui_scene_page_id()==0)
//					&& (!dsp_get_ui_scene_default_style())
//					&& (fr_system_get_watch_face() == 0))
//				{
//					if(!lv_obj_is_valid(prj_flow_cont))
//					{
//						prj_flow_cont = lv_obj_create(prj_parent_cont);
//						fr_lv_screensaver_func(prj_flow_cont, NULL);
//						screensaver_lock_flags = true;
//					}
//				}

			}
			break;
        default:
            {
                
            }
            break;
    }
}

// ui_func get_prior_element(uint8_t index)
// {
// 	return NULL;
//    // return fr_lv_get_prev_custom_page(index);
// }
// ui_func get_next_element(uint8_t index)
// {
// 	return NULL;
//    // return fr_lv_get_next_custom_page(index);
// }
// ui_func get_cur_element(uint8_t index)
// {
// 	return NULL;
//     //return fr_lv_get_cur_custom_page(index);
// }